<!DOCTYPE html>
<html>
<head hexo-theme='https://volantis.js.org/#2.6.3'>
  <meta charset="utf-8">
  <!-- SEO相关 -->
  
    
  
  <!-- 渲染优化 -->
  <meta name="renderer" content="webkit">
  <meta name="force-rendering" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="HandheldFriendly" content="True" >
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  <!-- 页面元数据 -->
  
    <title>为什么会有指针？[C语言] - 猿驿</title>
  
    <meta name="keywords" content="C语言,指针">
  
  
    <meta name="description" content="C语言指针学习">
  

  <!-- feed -->
  
    <link rel="alternate" href="/atom.xml" title="猿驿">
  

  <!-- import meta -->
  

  <!-- link -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13/css/all.min.css">
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">

  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.css">

  

  
  <link rel="shortcut icon" type='image/x-icon' href="https://gitee.com//BorenHU/BlogImage/raw/master/MarkDown/logo1.png">
  

  

  <!-- import link -->
  

  
    
      
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.18.1/styles/solarized-light.css">

    
  
  
    
<link rel="stylesheet" href="/css/style.css">

  

  <script>
    function setLoadingBarProgress(num) {
      document.getElementById('loading-bar').style.width=num+"%";
    }
  </script>

  
  
<style type="text/css" lang="css">
    #loading-container{
        position: fixed;
        top: 0;
        left: 0;
        min-height: 100vh;
        width: 100vw;
        z-index: 9999;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        background: #FFF;
        text-align: center;
        /* loader页面消失采用渐隐的方式*/
        -webkit-transition: opacity 1s ease;
        -moz-transition: opacity 1s ease;
        -o-transition: opacity 1s ease;
        transition: opacity 1s ease;
    }
    .loading-image{
        width: 120px;
        height: 50px;
        transform: translate(-50%);
    }

    .loading-image div:nth-child(2) {
        -webkit-animation: pacman-balls 1s linear 0s infinite;
        animation: pacman-balls 1s linear 0s infinite
    }

    .loading-image div:nth-child(3) {
        -webkit-animation: pacman-balls 1s linear .33s infinite;
        animation: pacman-balls 1s linear .33s infinite
    }

    .loading-image div:nth-child(4) {
        -webkit-animation: pacman-balls 1s linear .66s infinite;
        animation: pacman-balls 1s linear .66s infinite
    }

    .loading-image div:nth-child(5) {
        -webkit-animation: pacman-balls 1s linear .99s infinite;
        animation: pacman-balls 1s linear .99s infinite
    }

   .loading-image div:first-of-type {
        width: 0;
        height: 0;
        border: 25px solid #49b1f5;
        border-right-color: transparent;
        border-radius: 25px;
        -webkit-animation: rotate_pacman_half_up .5s 0s infinite;
        animation: rotate_pacman_half_up .5s 0s infinite;
    }
    .loading-image div:nth-child(2) {
        width: 0;
        height: 0;
        border: 25px solid #49b1f5;
        border-right-color: transparent;
        border-radius: 25px;
        -webkit-animation: rotate_pacman_half_down .5s 0s infinite;
        animation: rotate_pacman_half_down .5s 0s infinite;
        margin-top: -50px;
    }
    @-webkit-keyframes rotate_pacman_half_up {0% {transform: rotate(270deg)}50% {transform: rotate(1turn)}to {transform: rotate(270deg)}}

    @keyframes rotate_pacman_half_up {0% {transform: rotate(270deg)}50% {transform: rotate(1turn)}to {transform: rotate(270deg)}}

    @-webkit-keyframes rotate_pacman_half_down {0% {transform: rotate(90deg)}50% {transform: rotate(0deg)}to {transform: rotate(90deg)}}

    @keyframes rotate_pacman_half_down {0% {transform: rotate(90deg)}50% {transform: rotate(0deg)}to {transform: rotate(90deg)}}

    @-webkit-keyframes pacman-balls {75% {opacity: .7}to {transform: translate(-100px, -6.25px)}}

    @keyframes pacman-balls {75% {opacity: .7}to {transform: translate(-100px, -6.25px)}}


    .loading-image div:nth-child(3),
    .loading-image div:nth-child(4),
    .loading-image div:nth-child(5),
    .loading-image div:nth-child(6){
        background-color: #49b1f5;
        width: 15px;
        height: 15px;
        border-radius: 100%;
        margin: 2px;
        width: 10px;
        height: 10px;
        position: absolute;
        transform: translateY(-6.25px);
        top: 25px;
        left: 100px;
    }
    .loading-text{
        margin-bottom: 20vh;
        text-align: center;
        color: #2c3e50;
        font-size: 2rem;
        box-sizing: border-box;
        padding: 0 10px;
        text-shadow: 0 2px 10px rgba(0,0,0,0.2);
    }
    @media only screen and (max-width: 500px) {
         .loading-text{
            font-size: 1.5rem;
         }
    }
    .fadeout {
        opacity: 0;
        filter: alpha(opacity=0);
    }
    /* logo出现动画 */
    @-webkit-keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0);transform:translate3d(0,-100%,0)}100%{opacity:1;-webkit-transform:none;transform:none}}
    @keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0);}}
 </style>
 <script>
(function () {
    const loaded = function(){
       setTimeout(function(){
            const loader = document.getElementById("loading-container");
            loader.className="fadeout" ;//使用渐隐的方法淡出loading page
            // document.getElementById("body-wrap").style.display="flex";
            setTimeout(function(){
                loader.style.display="none";
            },1000); 
        },1000);//强制显示loading page 1s  
    };
    loaded();
})()
 </script><style type="text/css" lang="css">
    #loading-container{
        position: fixed;
        top: 0;
        left: 0;
        min-height: 100vh;
        width: 100vw;
        z-index: 9999;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        background: #FFF;
        text-align: center;
        /* loader页面消失采用渐隐的方式*/
        -webkit-transition: opacity 1s ease;
        -moz-transition: opacity 1s ease;
        -o-transition: opacity 1s ease;
        transition: opacity 1s ease;
    }
    .loading-image{
        width: 120px;
        height: 50px;
        transform: translate(-50%);
    }

    .loading-image div:nth-child(2) {
        -webkit-animation: pacman-balls 1s linear 0s infinite;
        animation: pacman-balls 1s linear 0s infinite
    }

    .loading-image div:nth-child(3) {
        -webkit-animation: pacman-balls 1s linear .33s infinite;
        animation: pacman-balls 1s linear .33s infinite
    }

    .loading-image div:nth-child(4) {
        -webkit-animation: pacman-balls 1s linear .66s infinite;
        animation: pacman-balls 1s linear .66s infinite
    }

    .loading-image div:nth-child(5) {
        -webkit-animation: pacman-balls 1s linear .99s infinite;
        animation: pacman-balls 1s linear .99s infinite
    }

   .loading-image div:first-of-type {
        width: 0;
        height: 0;
        border: 25px solid #49b1f5;
        border-right-color: transparent;
        border-radius: 25px;
        -webkit-animation: rotate_pacman_half_up .5s 0s infinite;
        animation: rotate_pacman_half_up .5s 0s infinite;
    }
    .loading-image div:nth-child(2) {
        width: 0;
        height: 0;
        border: 25px solid #49b1f5;
        border-right-color: transparent;
        border-radius: 25px;
        -webkit-animation: rotate_pacman_half_down .5s 0s infinite;
        animation: rotate_pacman_half_down .5s 0s infinite;
        margin-top: -50px;
    }
    @-webkit-keyframes rotate_pacman_half_up {0% {transform: rotate(270deg)}50% {transform: rotate(1turn)}to {transform: rotate(270deg)}}

    @keyframes rotate_pacman_half_up {0% {transform: rotate(270deg)}50% {transform: rotate(1turn)}to {transform: rotate(270deg)}}

    @-webkit-keyframes rotate_pacman_half_down {0% {transform: rotate(90deg)}50% {transform: rotate(0deg)}to {transform: rotate(90deg)}}

    @keyframes rotate_pacman_half_down {0% {transform: rotate(90deg)}50% {transform: rotate(0deg)}to {transform: rotate(90deg)}}

    @-webkit-keyframes pacman-balls {75% {opacity: .7}to {transform: translate(-100px, -6.25px)}}

    @keyframes pacman-balls {75% {opacity: .7}to {transform: translate(-100px, -6.25px)}}


    .loading-image div:nth-child(3),
    .loading-image div:nth-child(4),
    .loading-image div:nth-child(5),
    .loading-image div:nth-child(6){
        background-color: #49b1f5;
        width: 15px;
        height: 15px;
        border-radius: 100%;
        margin: 2px;
        width: 10px;
        height: 10px;
        position: absolute;
        transform: translateY(-6.25px);
        top: 25px;
        left: 100px;
    }
    .loading-text{
        margin-bottom: 20vh;
        text-align: center;
        color: #2c3e50;
        font-size: 2rem;
        box-sizing: border-box;
        padding: 0 10px;
        text-shadow: 0 2px 10px rgba(0,0,0,0.2);
    }
    @media only screen and (max-width: 500px) {
         .loading-text{
            font-size: 1.5rem;
         }
    }
    .fadeout {
        opacity: 0;
        filter: alpha(opacity=0);
    }
    /* logo出现动画 */
    @-webkit-keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0);transform:translate3d(0,-100%,0)}100%{opacity:1;-webkit-transform:none;transform:none}}
    @keyframes fadeInDown{0%{opacity:0;-webkit-transform:translate3d(0,-100%,0);}}
 </style>
 <script>
(function () {
    const loaded = function(){
       setTimeout(function(){
            const loader = document.getElementById("loading-container");
            loader.className="fadeout" ;//使用渐隐的方法淡出loading page
            // document.getElementById("body-wrap").style.display="flex";
            setTimeout(function(){
                loader.style.display="none";
            },1000); 
        },1000);//强制显示loading page 1s  
    };
    loaded();
})()
 </script></head>


 <div id="loading-container">
     <p class="loading-text">小生努力加载中 . . . </p> 
     <div class="loading-image">
         <div></div>
         <div></div>
         <div></div>
         <div></div> 
         <div></div>
     </div>
 </div><body>
  
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>
<header class="l_header shadow floatable">
  <div class='container'>
  <div class='wrapper'>
    <div class='nav-sub'>
      <p class="title"></p>
      <ul class='switcher nav-list-h'>
        <li><a class="s-comment fas fa-comments fa-fw" target="_self" href='javascript:void(0)'></a></li>
        
          <li><a class="s-toc fas fa-list fa-fw" target="_self" href='javascript:void(0)'></a></li>
        
      </ul>
    </div>
		<div class="nav-main">
      
        
        <a class="title flat-box" target="_self" href='/'>
          
          
            <i class='https://gitee.com//BorenHU/BlogImage/raw/master/MarkDown/logo1.png'></i>
          
          
            ApePost
          
          
        </a>
      

			<div class='menu navigation'>
				<ul class='nav-list-h'>
          
          
          
            
            
              <li>
                <a class="flat-box" href=/
                  
                  
                  
                    id="home"
                  >
                  <i class='fas fa-home fa-fw'></i>博客
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=https://www.niupian8.com/
                  
                  
                    target="_blank"
                  
                  
                    id="https:wwwniupian8com"
                  >
                  <i class='fas fa-film fa-fw'></i>快播吧
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/categories/
                  
                  
                  
                    id="categories"
                  >
                  <i class='fas fa-folder-open fa-fw'></i>分类
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/tags/
                  
                  
                  
                    id="tags"
                  >
                  <i class='fas fa-tags fa-fw'></i>标签
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/archives/
                  
                  
                  
                    id="archives"
                  >
                  <i class='fas fa-archive fa-fw'></i>归档
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/friends/
                  
                  
                  
                    id="friends"
                  >
                  <i class='fas fa-link fa-fw'></i>友链
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/about/
                  
                  
                  
                    id="about"
                  >
                  <i class='fas fa-info-circle fa-fw'></i>关于
                </a>
                
              </li>
            
          
          
				</ul>
			</div>

      <div class="m_search">
        <form name="searchform" class="form u-search-form">
          <i class="icon fas fa-search fa-fw"></i>
          <input type="text" class="input u-search-input" placeholder="Search..." />
        </form>
      </div>

			<ul class='switcher nav-list-h'>
				
					<li><a class="s-search fas fa-search fa-fw" target="_self" href='javascript:void(0)'></a></li>
				
				<li>
          <a class="s-menu fas fa-bars fa-fw" target="_self" href='javascript:void(0)'></a>
          <ul class="menu-phone list-v navigation white-box">
            
              
            
              <li>
                <a class="flat-box" href=/
                  
                  
                  
                    id="home"
                  >
                  <i class='fas fa-home fa-fw'></i>博客
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=https://www.niupian8.com/
                  
                  
                    target="_blank"
                  
                  
                    id="https:wwwniupian8com"
                  >
                  <i class='fas fa-film fa-fw'></i>快播吧
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/categories/
                  
                  
                  
                    id="categories"
                  >
                  <i class='fas fa-folder-open fa-fw'></i>分类
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/tags/
                  
                  
                  
                    id="tags"
                  >
                  <i class='fas fa-tags fa-fw'></i>标签
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/archives/
                  
                  
                  
                    id="archives"
                  >
                  <i class='fas fa-archive fa-fw'></i>归档
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/friends/
                  
                  
                  
                    id="friends"
                  >
                  <i class='fas fa-link fa-fw'></i>友链
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/about/
                  
                  
                  
                    id="about"
                  >
                  <i class='fas fa-info-circle fa-fw'></i>关于
                </a>
                
              </li>
            
          
            
          </ul>
        </li>
			</ul>
		</div>
	</div>
  </div>
</header>

<script>setLoadingBarProgress(40);</script>



  <div class="l_body nocover">
    <div class='body-wrapper'>
      

<div class='l_main'>
  

  
    <article id="post" class="post white-box reveal shadow floatable article-type-post" itemscope itemprop="blogPost">
      


  <section class='meta'>
    
      
      
      <div class="meta" id="header-meta">
        
          
  <h1 class="title">
    <a href="/posts/26235.html">
      为什么会有指针？[C语言]
    </a>
  </h1>


        
        <div class='new-meta-box'>
          
            
          
            
              
<div class='new-meta-item author'>
  <a href="https://www.huqifa.com/" target="_blank" rel="nofollow noopener">
    <img src="/images/loading.gif" data-original="https://gitee.com//BorenHU/BlogImage/raw/master/MarkDown/11.jpg">
    <p>JF</p>
  </a>
</div>

            
          
            
              
  
  <div class='new-meta-item category'>
    <a href='/categories/C%E8%AF%AD%E8%A8%80/' rel="nofollow">
      <i class="fas fa-folder-open fa-fw" aria-hidden="true"></i>
      <p>C语言</p>
    </a>
  </div>


            
          
            
          
            
              <div class="new-meta-item date">
  <a class='notlink'>
    <i class="fas fa-calendar-alt fa-fw" aria-hidden="true"></i>
    <p>发布于：2020年3月31日</p>
  </a>
</div>

            
          
            
              

            
          
        </div>
        
          <hr>
        
      </div>
    
  </section>


      <section class="article typo">
        <div class="article-entry" itemprop="articleBody">
          
          <p>C语言指针学习</p>
<a id="more"></a>

<hr>
<h2 id=""><a href="#" class="headerlink" title=""></a></h2><h3 id="1-为什么会有指针？这门技术解决了什么问题？"><a href="#1-为什么会有指针？这门技术解决了什么问题？" class="headerlink" title="1. 为什么会有指针？这门技术解决了什么问题？"></a>1. 为什么会有指针？这门技术解决了什么问题？</h3><p>转载：<a href="https://blog.csdn.net/dangbai01_/article/details/79501037" target="_blank" rel="noopener">https://blog.csdn.net/dangbai01_/article/details/79501037</a></p>
<p><strong>（0）史前</strong></p>
<p>早期的CPU（也许并没有真正的实现）并不如今天的强大，</p>
<p>内存读写的指令(命令)只有:</p>
<p>“从  常数地址0x1234  地址处读入1字节到  寄存器a”，</p>
<p>或者“把  寄存器b  的值写入  常数地址0x5678  这个地方”。</p>
<p>而如果你想清空100字节的内存，每条命令只能对内存中某一个字节进行写入，那就得写100条命令。随着要处理的数据的膨胀，程序也得跟着膨胀，而这是不可接受的。</p>
<p><strong>(1）流程跳转</strong></p>
<p>为了解决清空大量内存的问题，人们发明了控制流程的命令</p>
<p>比如“寄存器c的值为0，则跳过下一条指令” 或 “无条件跳转到首地址为0x9012”的地方，从那个地方继续运行”。</p>
<p>注意，命令本身也是编码成字节存在内存里的。于是就可以写选择，循环等控制语句。</p>
<p><strong>(2）自修改程序</strong></p>
<p>但是问题并没有解决，因为就算有了选择循环结构，<strong>整个程序还是只能写常量</strong>，所以如果想要对不同的内存块清零，只能写不同的命令。</p>
<p>但是人是很聪明的。</p>
<p>利用“命令本身也是编码成字节存在内存里”这个特征，我们可以通过修改内存来修改命令，继而修改行为状态。</p>
<p>举例来说，在0x3454地址处存了一条命令，这条命令叫做“把0x5678开始的一字节清零”。</p>
<p>假设这条指令占用了三个字节，第一个字节告诉CPU这是一条清零指令，后两个字节（0x3455和0x3456）存了一个表示地址的整数，告诉CPU到底要把哪个字节清零。显然，这个整数会和0x5678有关。</p>
<p>现在，要是CPU在某处执行了一句“把起始地址为0x3455的那个2字节数自增1”，那实际上0x3454处的命令就改成了“把0x5679（0x5678+1）开始的一字节清零”。</p>
<p>看到希望了么，我们可以写一个命令，然后不断修改这个命令，再加上流程控制的命令产生一个循环，就能用一段固定长度（注意，代码内容随着执行并不固定）的代码清零任意指定的一段内存块！</p>
<p>这叫做“自修改程序”。</p>
<p><strong>(3）间接寻址</strong></p>
<p>后来的事情就很简单了。编写这种自修改程序极容易出错，因为稍微改错一个地方命令就全改乱了，比如在上例中如果把0x3454中的数字给改了，就完全变成了另一条命令，</p>
<p>所以人们再次发明新的工具，叫  间接寻址。</p>
<p><strong>(4）指针</strong></p>
<p>哦，现在解释指针就很简单了，指针就是间接寻址里面的内存块。它存了一片地址，而指针解引用（比如*p）就对应的是间接寻址读写的指令了。</p>
<h3 id="2-指针这项技术如何使用？"><a href="#2-指针这项技术如何使用？" class="headerlink" title="2. 指针这项技术如何使用？"></a>2. 指针这项技术如何使用？</h3><p>假如你申请一个整型变量 int a = 5；相当于把5赋值给a这个房子里，而&amp;a就是这个房子的地址，而指针指向的就是房子的地址</p>
<p>而要搞清一个指针需要搞清指针的四方面的内容：</p>
<ol>
<li><p><strong>指针的类型</strong></p>
</li>
<li><p><strong>指针所指向的类型</strong></p>
</li>
<li><p><strong>指针的值（指针所指向的内存区）</strong></p>
</li>
<li><p><strong>指针本身所占据的内存区。</strong></p>
</li>
</ol>
<p>先声明几个指针放着做例子：</p>
<p>例一：</p>
<p>(1)int *ptr;</p>
<p>(2)char *ptr;</p>
<p>(3)int **ptr;</p>
<p>(4)int (*ptr)[3];</p>
<p>(5)int <em>(</em>ptr)[4];</p>
<ul>
<li><strong>指针的类型。</strong></li>
</ul>
<p>从语法的角度看，你只要把指针声明语句里的指针名字去掉，剩下的部分就是这个指针的类型。这是指针<br>本身所具有的类型。让我们看看例一中各个指针的类型：</p>
<ol>
<li><p>int *ptr; //指针的类型是int *</p>
</li>
<li><p>char *ptr; //指针的类型是char *</p>
</li>
<li><p>int ** ptr; //指针的类型是 int **</p>
</li>
<li><p>int (<em>ptr)[3]; //指针的类型是 int(</em>)[3]</p>
</li>
<li><p>int <em>(</em>ptr)[4]; //指针的类型是 int <em>(</em>)[4]</p>
</li>
</ol>
<p>怎么样？找出指针的类型的方法是不是很简单？</p>
<ul>
<li><strong>指针所指向的类型</strong></li>
</ul>
<p>当你通过指针来访问指针所指向的内存区时，指针所指向的类型决定了编译器将把那片内存区里的内容当<br>做什么来看待。</p>
<p>从语法上看，你只须把指针声明语句中的指针名字和名字左边的指针声明符*去掉，剩下的就是指针所指<br>向的类型。例如：</p>
<p>(1)int *ptr; //指针所指向的类型是int</p>
<p>(2)char *ptr; //指针所指向的的类型是char</p>
<p>(3)int **ptr; //指针所指向的的类型是 int *</p>
<p>(4)int (*ptr)[3]; //指针所指向的的类型是 int()[3]</p>
<p>(5)int *(*ptr)[4]; //指针所指向的的类型是 int *()[4]</p>
<p>在指针的算术运算中，指针所指向的类型有很大的作用。</p>
<p>指针的类型(即指针本身的类型)和指针所指向的类型是两个概念。当你对C越来越熟悉时，你会发现，把<br>与指针搅和在一起的“类型”这个概念分成“指针的类型”和“指针所指向的类型”两个概念，是精通指针的关键点之一。</p>
<ul>
<li><strong>指针的值，或者叫指针所指向的内存区或地址</strong></li>
</ul>
<p>指针的值是指针 本身存储的数值，这个值将被编译器当作一个地址，而不是一个一般的数值。在32位程序<br>里，所有类型的指针的值都是一个32位整数，因为32位程序里<a href="https://www.baidu.com/s?wd=内存地址&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" target="_blank" rel="noopener">内存地址</a>全都是32位长。</p>
<p>指针所指向的内存区就是从指针的值所代表的那个<a href="https://www.baidu.com/s?wd=内存地址&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" target="_blank" rel="noopener">内存地址</a>开始，长度为sizeof(指针所指向的类型)的一<br>片内存区。以后，我们说一个指针的值是XX，就相当于说该指针指向了以XX为首地址的一片内存区域；</p>
<p>我们说一个指针指向了某块内存区域，就相当于说该指针的值是这块内存区域的首地址。</p>
<p>指针所指向的内存区和指针所指向的类型是两个完全不同的概念。在例一中，指针所指向的类型已经有<br>了，但由于指针还未初始化，所以它所指向的内存区是不存在的，或者说是无意义的。</p>
<p>以后，每遇到一个指针，都应该问问：</p>
<p>这个指针的类型是什么？指针指向的类型是什么？该指针指向了哪<br>里？</p>
<ul>
<li><strong>指针本身所占据的内存区</strong></li>
</ul>
<p>指针本身占了多大的内存？你只要用函数sizeof(指针的类型)测一下就知道了。在32位平台里，指针本身<br>占据了4个字节的长度。</p>
<p>指针本身占据的内存这个概念在判断一个指针表达式是否是左值时很有用。</p>
<h4 id="指针的算术运算"><a href="#指针的算术运算" class="headerlink" title="指针的算术运算"></a>指针的算术运算</h4><p>指针可以加上或减去一个整数。指针的这种运算的意义和通常的数值的加减运算的意义是不一样的。例<br>如：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">char</span> a<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token operator">*</span>ptr<span class="token operator">=</span>a<span class="token punctuation">;</span>

ptr<span class="token operator">++</span><span class="token punctuation">;</span></code></pre>
<p>在上例中，指针ptr的类型是int<em>它指向的类型是int，它被初始化为指向整形变量a。接下来的第3句<br>中，指针ptr被加了1，编译器是这样处理的：*</em>它把指针ptr的值加上了sizeof(int)，在32位程序中，是被<br>加上了4。由于地址是用字节做单位的，故ptr所指向的地址由原来的变量a的地址向高地址方向增加了4个<br>字节。**</p>
<p>由于char类型的长度是一个字节，所以，原来ptr是指向数组a的第0号单元开始的四个字节，此时指向了<br>数组a中从第4号单元开始的四个字节。</p>
<p>我们可以用一个指针和一个循环来遍历一个数组，看例子：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> array<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token operator">*</span>ptr<span class="token operator">=</span>array<span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token comment" spellcheck="true">//此处略去为整型数组赋值的代码。</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">20</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token punctuation">(</span><span class="token operator">*</span>ptr<span class="token punctuation">)</span><span class="token operator">++</span><span class="token punctuation">;</span>
ptr<span class="token operator">++</span>；
<span class="token punctuation">}</span></code></pre>
<p>这个例子将整型数组中各个单元的值加1。由于每次循环都将指针ptr加1，所以每次循环都能访问数组的<br>下一个单元。再看例子：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">char</span> a<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token operator">*</span>ptr<span class="token operator">=</span>a<span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

ptr<span class="token operator">+</span><span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">;</span></code></pre>
<p>在这个例子中，ptr被加上了5，编译器是这样处理的：将指针ptr的值加上5乘sizeof(int)，在32位程序<br>中就是加上了5乘4=20。由于地址的单位是字节，故现在的ptr所指向的地址比起加5后的ptr所指向的地址<br>来说，向高地址方向移动了20个字节。在这个例子中，没加5前的ptr指向数组a的第0号单元开始的四个字<br>节，加5后，ptr已经指向了数组a的合法范围之外了。虽然这种情况在应用上会出问题，但在语法上却是<br>可以的。这也体现出了指针的灵活性。</p>
<h4 id="运算符-amp-和"><a href="#运算符-amp-和" class="headerlink" title="*运算符&amp;和\ **"></a><em>*运算符&amp;和\</em> **</h4><p>这里&amp;是取地址运算符,…书上叫做“间接运算符”。&amp;a的运算结果是一个指针，指针的类型是a的类<br>型加个*，指针所指向的类型是a的类型，指针所指向的地址嘛，那就是a的地址。*p的运算结果就五花八<br>门了。总之*p的结果是p所指向的东西，这个东西有这些特点：它的类型是p指向的类型，它所占用的地址<br>是p所指向的地址。</p>
<pre class=" language-c"><code class="language-c">例五：

<span class="token keyword">int</span> a<span class="token operator">=</span><span class="token number">12</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> b<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token operator">*</span>p<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token operator">*</span><span class="token operator">*</span>ptr<span class="token punctuation">;</span></code></pre>
<p>p=&amp;a;</p>
<p>//&amp;a的结果是一个指针，类型是int*，指向的类型是int，指向的地址是a的地址。</p>
<p>*p=24;</p>
<p>//*p的结果，在这里它的类型是int，它所占用的地址是p所指向的地址，显然，*p就是变量a。</p>
<p>ptr=&amp;p;</p>
<p>//&amp;p的结果是个指针，该指针的类型是p的类型加个*，在这里是int*。该指针所指向的类型是p<br>的类型，这里是int*。该指针所指向的地址就是指针p自己的地址。</p>
<p>*ptr=&amp;b;</p>
<p>//*ptr是个指针，&amp;b的结果也是个指针，且这两个指针的类型和所指向的类型是一样的，所以用<br>&amp;b来给*ptr赋值就是毫无问题的了。</p>
<p>**ptr=34;</p>
<p>//*ptr的结果是ptr所指向的东西，在这里是一个指针，对这个指针再做一次*运算，结果就是<br>一个int类型的变量。</p>
<h4 id="指针表达式"><a href="#指针表达式" class="headerlink" title="指针表达式"></a><strong>指针表达式</strong></h4><p>一个表达式的最后结果如果是一个指针，那么这个表达式就叫指针表达式。下面是一些指针表达式的例<br>子：</p>
<pre class=" language-c"><code class="language-c">例六：

<span class="token keyword">int</span> a<span class="token punctuation">,</span>b<span class="token punctuation">;</span>

<span class="token keyword">int</span> array<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token operator">*</span>pa<span class="token punctuation">;</span>

pa<span class="token operator">=</span><span class="token operator">&amp;</span>a<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//&amp;a是一个指针表达式。</span>

<span class="token keyword">int</span> <span class="token operator">*</span><span class="token operator">*</span>ptr<span class="token operator">=</span><span class="token operator">&amp;</span>pa<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//&amp;pa也是一个指针表达式。</span>

<span class="token operator">*</span>ptr<span class="token operator">=</span><span class="token operator">&amp;</span>b<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//*ptr和&amp;b都是指针表达式。</span>

pa<span class="token operator">=</span>array<span class="token punctuation">;</span>

pa<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//这也是指针表达式。</span>

例七：

<span class="token keyword">char</span> <span class="token operator">*</span>arr<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">char</span> <span class="token operator">*</span>str<span class="token punctuation">;</span>

str<span class="token operator">=</span><span class="token operator">*</span>parr<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//*parr是指针表达式</span>

str<span class="token operator">=</span><span class="token operator">*</span><span class="token punctuation">(</span>parr<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//*(parr+1)是指针表达式</span>

str<span class="token operator">=</span><span class="token operator">*</span><span class="token punctuation">(</span>parr<span class="token operator">+</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//*(parr+2)是指针表达式</span></code></pre>
<p>由于指针表达式的结果是一个指针，所以指针表达式也具有指针所具有的四个要素：指针的类型，指针所<br>指向的类型，指针指向的内存区，指针自身占据的内存。</p>
<p>好了，当一个指针表达式的结果指针已经明确地具有了指针自身占据的内存的话，这个指针表达式就是一<br>个左值，否则就不是一个左值。</p>
<p>在例七中，&amp;a不是一个左值，因为它还没有占据明确的内存。*ptr是一个左值，因为*ptr这个指针已经占<br>据了内存，其实*ptr就是指针pa，既然pa已经在内存中有了自己的位置，那么*ptr当然也有了自己的位<br>置。</p>
<h4 id="数组和指针的关系"><a href="#数组和指针的关系" class="headerlink" title="数组和指针的关系"></a><strong>数组和指针的关系</strong></h4><pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> array<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">,</span>value<span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
value<span class="token operator">=</span>array<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//也可写成：value=*array;</span>
value<span class="token operator">=</span>array<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//也可写成：value=*(array+3);</span>
value<span class="token operator">=</span>array<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//也可写成：value=*(array+4);</span></code></pre>
<p>上例中，一般而言数组名array代表数组本身，类型是int [10]，但如果把array看做指针的话，它指向数<br>组的第0个单元，类型是int *，所指向的类型是数组单元的类型即int。因此*array等于0就一点也不奇怪<br>了。同理，array+3是一个指向数组第3个单元的指针，所以*(array+3)等于3。其它依此类推。</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">char</span> <span class="token operator">*</span>str<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">{</span>
<span class="token string">"Hello,this is a sample!"</span><span class="token punctuation">,</span>
<span class="token string">"Hi,good morning."</span><span class="token punctuation">,</span>
<span class="token string">"Hello world"</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">char</span> s<span class="token punctuation">[</span><span class="token number">80</span><span class="token punctuation">]</span>；
<span class="token function">strcpy</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>str<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//也可写成strcpy(s,*str);</span>
<span class="token function">strcpy</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>str<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//也可写成strcpy(s,*(str+1));</span>
<span class="token function">strcpy</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span>str<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//也可写成strcpy(s,*(str+2));</span></code></pre>
<p>上例中，str是一个三单元的数组，该数组的每个单元都是一个指针，这些指针各指向一个字符串。把指<br>针数组名str当作一个指针的话，它指向数组的第0号单元，它的类型是char**，它指向的类型是char *。</p>
<p>*str也是一个指针，它的类型是char*，它所指向的类型是char，它指向的地址是字符串”Hello,this is<br>a sample!”的第一个字符的地址，即’H’的地址。</p>
<p>str+1也是一个指针，它指向数组的第1号单元，它的类型是char**，它指向的类型是char *。</p>
<p>*(str+1)也是一个指针，它的类型是char*，它所指向的类型是char，它指向”Hi,good morning.”的第一<br>个字符’H’，等等。</p>
<p>下面总结一下数组的数组名的问题。声明了一个数组TYPE array[n]，则数组名称array就有了两重含义：</p>
<ul>
<li><p>第一，它代表整个数组，它的类型是TYPE [n]；</p>
</li>
<li><p>第二，它是一个指针，该指针的类型是TYPE*，该指针指<br>向的类型是TYPE，也就是数组单元的类型，该指针指向的内存区就是数组第0号单元，该指针自己占有单<br>独的内存区，注意它和数组第0号单元占据的内存区是不同的。该指针的值是不能修改的，即类似array++<br>的表达式是错误的。</p>
</li>
</ul>
<p>在不同的表达式中数组名array可以扮演不同的角色。</p>
<p>在表达式sizeof(array)中，数组名array代表数组本身，故这时sizeof函数测出的是整个数组的大小。</p>
<p>在表达式*array中，array扮演的是指针，因此这个表达式的结果就是数组第0号单元的值。sizeof<br>(*array)测出的是数组单元的大小。</p>
<p>表达式array+n（其中n=0，1，2，….。）中，array扮演的是指针，故array+n的结果是一个指针，它的<br>类型是TYPE*，它指向的类型是TYPE，它指向数组第n号单元。故sizeof(array+n)测出的是指针类型的大<br>小。</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> array<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>ptr<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
ptr<span class="token operator">=</span><span class="token operator">&amp;</span>array<span class="token punctuation">;</span></code></pre>
<p>上例中ptr是一个指针，它的类型是int (*)[10]，他指向的类型是int [10]，我们用整个数组的首地址来<br>初始化它。在语句ptr=&amp;array中，array代表数组本身。</p>
<p>本节中提到了函数sizeof()，那么我来问一问，sizeof(指针名称)测出的究竟是指针自身类型的大小呢还<br>是指针所指向的类型的大小？答案是前者。例如：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>ptr<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span></code></pre>
<p>则在32位程序中，有：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">4</span>
<span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">int</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">40</span>
<span class="token keyword">sizeof</span><span class="token punctuation">(</span>ptr<span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">4</span></code></pre>
<p>实际上，sizeof(对象)测出的都是对象自身的类型的大小，而不是别的什么类型的大小。</p>
<h4 id="指针和结构类型的关系"><a href="#指针和结构类型的关系" class="headerlink" title="指针和结构类型的关系"></a><strong>指针和结构类型的关系</strong></h4><p>可以声明一个指向结构类型对象的指针。</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">struct</span> MyStruct
<span class="token punctuation">{</span>
<span class="token keyword">int</span> a<span class="token punctuation">;</span>
<span class="token keyword">int</span> b<span class="token punctuation">;</span>
<span class="token keyword">int</span> c<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>MyStruct ss;</p>
<p>//声明了结构对象ss，并把ss的三个成员初始化为20，30和40。</p>
<p>MyStruct *ptr=&amp;ss;</p>
<p>//声明了一个指向结构对象ss的指针。它的类型是</p>
<p>MyStruct*,它指向的类型是MyStruct。</p>
<p>int *pstr=(int*)&amp;ss;</p>
<p>//声明了一个指向结构对象ss的指针。但是它的类型和它指向的类型和ptr是不同<br>的。</p>
<p>请问怎样通过指针ptr来访问ss的三个成员变量？</p>
<p>答案：</p>
<p>ptr-&gt;a;</p>
<p>ptr-&gt;b;</p>
<p>ptr-&gt;c;</p>
<p>又请问怎样通过指针pstr来访问ss的三个成员变量？</p>
<p>答案：</p>
<p>*pstr；//访问了ss的成员a。</p>
<p>*(pstr+1);//访问了ss的成员b。</p>
<p>*(pstr+2)//访问了ss的成员c。</p>
<p>虽然我在我的MSVC++6.0上调式过上述代码，但是要知道，这样使用pstr来访问结构成员是不正规<br>的，为了说明为什么不正规，让我们看看怎样通过指针来访问数组的各个单元：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> array<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token operator">*</span>pa<span class="token operator">=</span>array<span class="token punctuation">;</span>

通过指针pa访问数组array的三个单元的方法是：

<span class="token operator">*</span>pa<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//访问了第0号单元</span>

<span class="token operator">*</span><span class="token punctuation">(</span>pa<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//访问了第1号单元</span>

<span class="token operator">*</span><span class="token punctuation">(</span>pa<span class="token operator">+</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//访问了第2号单元</span>

从格式上看倒是与通过指针访问结构成员的不正规方法的格式一样。</code></pre>
<h4 id="指针和函数的关系"><a href="#指针和函数的关系" class="headerlink" title="指针和函数的关系"></a><strong>指针和函数的关系</strong></h4><p>可以把一个指针声明成为一个指向函数的指针。</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">fun1</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>pfun1<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

pfun1<span class="token operator">=</span>fun1<span class="token punctuation">;</span>

<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

<span class="token keyword">int</span> a<span class="token operator">=</span><span class="token punctuation">(</span><span class="token operator">*</span>pfun1<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token string">"abcdefg"</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//通过函数指针调用函数。</span></code></pre>
<p>可以把指针作为函数的形参。在函数调用语句中，可以用指针表达式来作为实参。</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> a<span class="token punctuation">;</span>
<span class="token keyword">char</span> str<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"abcdefghijklmn"</span><span class="token punctuation">;</span>
a<span class="token operator">=</span><span class="token function">fun</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">int</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span>s<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">int</span> num<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i
<span class="token punctuation">{</span>
num<span class="token operator">+</span><span class="token operator">=</span><span class="token operator">*</span>s<span class="token punctuation">;</span>s<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> num<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>这个例子中的函数fun统计一个字符串中各个字符的ASCII码值之和。前面说了，数组的名字也是一个指<br>针。在函数调用中，当把str作为实参传递给形参s后，实际是把str的值传递给了s，s所指向的地址就和<br>str所指向的地址一致，但是str和s各自占用各自的存储空间。在函数体内对s进行自加1运算，并不意味<br>着同时对str进行了自加1运算。</p>
<h4 id="指针类型转换"><a href="#指针类型转换" class="headerlink" title="指针类型转换"></a><strong>指针类型转换</strong></h4><p>当我们初始化一个指针或给一个指针赋值时，赋值号的左边是一个指针，赋值号的右边是一个指针表达<br>式。在我们前面所举的例子中，绝大多数情况下，指针的类型和指针表达式的类型是一样的，指针所指向<br>的类型和指针表达式所指向的类型是一样的。</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">float</span> f<span class="token operator">=</span><span class="token number">12.3</span><span class="token punctuation">;</span>
<span class="token keyword">float</span> <span class="token operator">*</span>fptr<span class="token operator">=</span><span class="token operator">&amp;</span>f<span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token operator">*</span>p<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//在上面的例子中，假如我们想让指针p指向实数f，应该怎么搞？是用下面的语句吗？</span>
p<span class="token operator">=</span><span class="token operator">&amp;</span>f<span class="token punctuation">;</span></code></pre>
<p>不对。因为指针p的类型是int*，它指向的类型是int。表达式&amp;f的结果是一个指针，指针的类型是<br>float*,它指向的类型是float。两者不一致，直接赋值的方法是不行的。</p>
<p>p=(int*)&amp;f;</p>
<p>如果有一个指针p，我们需要把它的类型和所指向的类型改为TYEP*和TYPE，那么语法格式是：</p>
<p>(TYPE*)p；</p>
<p>这样强制类型转换的结果是一个新指针，该新指针的类型是TYPE*，它指向的类型是TYPE，它指向的地址<br>就是原指针指向的地址。而原来的指针p的一切属性都没有被修改。</p>
<p>一个函数如果使用了指针作为形参，那么在函数调用语句的实参和形参的结合过程中，也会发生指针类型<br>的转换。</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> a<span class="token operator">=</span><span class="token number">125</span><span class="token punctuation">,</span>b<span class="token punctuation">;</span>
<span class="token function">fun</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">void</span> <span class="token function">fun</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span>s<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">char</span> c<span class="token punctuation">;</span>
c<span class="token operator">=</span><span class="token operator">*</span><span class="token punctuation">(</span>s<span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">*</span><span class="token punctuation">(</span>s<span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">*</span><span class="token punctuation">(</span>s<span class="token operator">+</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">*</span><span class="token punctuation">(</span>s<span class="token operator">+</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token operator">=</span>c<span class="token punctuation">;</span>
c<span class="token operator">=</span><span class="token operator">*</span><span class="token punctuation">(</span>s<span class="token operator">+</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">*</span><span class="token punctuation">(</span>s<span class="token operator">+</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">*</span><span class="token punctuation">(</span>s<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">*</span><span class="token punctuation">(</span>s<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">=</span>c<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>注意这是一个32位程序，故int类型占了四个字节，char类型占一个字节。</p>
<p>函数fun的作用是把一个整数的<br>四个字节的顺序来个颠倒。注意到了吗？</p>
<p>在函数调用语句中，实参&amp;a的结果是一个指针，它的类型是int*，它指向的类型是int。形参这个指针的类型是char*，它指向的类型是char。</p>
<p>这样，在实参和形参的结合过程中，我们必须进行一次从int*类型到char*类型的转换。结合这个例子，我们可以这样来想象编译器进行转换的过程：编译器先构造一个临时指针 char* temp，然后执行temp=(char * )&amp;a，最后再把temp的<br>值传递给s。</p>
<p>所以最后的结果是：s的类型是char*,它指向的类型是char，它指向的地址就是a的首地址。</p>
<p>我们已经知道，指针的值就是指针指向的地址，在32位程序中，指针的值其实是一个32位整数。那可不可<br>以把一个整数当作指针的值直接赋给指针呢？就象下面的语句：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">unsigned</span> <span class="token keyword">int</span> a<span class="token punctuation">;</span>
TYPE <span class="token operator">*</span>ptr<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//TYPE是int，char或结构类型等等类型。</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
a<span class="token operator">=</span><span class="token number">20345686</span><span class="token punctuation">;</span>
ptr<span class="token operator">=</span><span class="token number">20345686</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//我们的目的是要使指针ptr指向地址20345686（十进制）</span>
ptr<span class="token operator">=</span>a<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//我们的目的是要使指针ptr指向地址20345686（十进制）</span></code></pre>
<p>编译一下吧。结果发现后面两条语句全是错的。那么我们的目的就不能达到了吗？不，还有办法：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">unsigned</span> <span class="token keyword">int</span> a<span class="token punctuation">;</span>
TYPE <span class="token operator">*</span>ptr<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//TYPE是int，char或结构类型等等类型。</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
a<span class="token operator">=</span>某个数，这个数必须代表一个合法的地址；
ptr<span class="token operator">=</span><span class="token punctuation">(</span>TYPE<span class="token operator">*</span><span class="token punctuation">)</span>a；<span class="token comment" spellcheck="true">//呵呵，这就可以了。</span></code></pre>
<p>严格说来这里的(TYPE*)和指针类型转换中的(TYPE*)还不一样。这里的(TYPE*)的意思是把<a href="https://www.baidu.com/s?wd=无符号整数&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" target="_blank" rel="noopener">无符号整数</a>a的<br>值当作一个地址来看待。</p>
<p>上面强调了a的值必须代表一个合法的地址，否则的话，在你使用ptr的时候，就会出现<a href="https://www.baidu.com/s?wd=非法操作&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" target="_blank" rel="noopener">非法操作</a>错误。</p>
<p>想想能不能反过来，把指针指向的地址即指针的值当作一个整数取出来。完全可以。下面的例子演示了把<br>一个指针的值当作一个整数取出来，然后再把这个整数当作一个地址赋给一个指针：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> a<span class="token operator">=</span><span class="token number">123</span><span class="token punctuation">,</span>b<span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token operator">*</span>ptr<span class="token operator">=</span><span class="token operator">&amp;</span>a<span class="token punctuation">;</span>
<span class="token keyword">char</span> <span class="token operator">*</span>str<span class="token punctuation">;</span>
b<span class="token operator">=</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>ptr<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//把指针ptr的值当作一个整数取出来。</span>
str<span class="token operator">=</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>b<span class="token punctuation">;</span><span class="token comment" spellcheck="true">//把这个整数的值当作一个地址赋给指针str。</span></code></pre>
<p>好了，现在我们已经知道了，可以把指针的值当作一个整数取出来，也可以把一个整数值当作地址赋给一<br>个指针。</p>
<script>
        document.querySelectorAll('.github-emoji')
          .forEach(el => {
            if (!el.dataset.src) { return; }
            const img = document.createElement('img');
            img.style = 'display:none !important;';
            img.src = el.dataset.src;
            img.addEventListener('error', () => {
              img.remove();
              el.style.color = 'inherit';
              el.style.backgroundImage = 'none';
              el.style.background = 'none';
            });
            img.addEventListener('load', () => {
              img.remove();
            });
            document.body.appendChild(img);
          });
      </script>
          
            <div class='article_footer'>
              
                
  
    
    



  

  
    
    



  

  
    
    

<section class="widget copyright  desktop mobile">
  <div class='content'>
    
      <blockquote>
        
          
            <p>博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</p>

          
        
          
            <p>作者：JF</p>

          
        
          
            <p>本文永久链接是：<a href=https://huqifa.com/posts/26235.html>https://huqifa.com/posts/26235.html</a></p>
          
        
      </blockquote>
    
  </div>
</section>

  

  
    
    

<section class="widget qrcode  desktop">
  

  <div class='content article-entry'>
    
      
        <div class='fancybox'><img src='https://gitee.com//BorenHU/BlogImage/raw/master/MarkDown/wechat.png'
        
          height='64px'
        ></div>
      
    
      
        <div class='fancybox'><img src='https://gitee.com//BorenHU/BlogImage/raw/master/MarkDown/alipay.jpg'
        
          height='64px'
        ></div>
      
    
  </div>
</section>

  


              
            </div>
          
        </div>
        
          


  <section class='meta' id="footer-meta">
    <div class='new-meta-box'>
      
        
          <div class="new-meta-item date" itemprop="dateUpdated" datetime="2020-04-26T12:46:01+08:00">
  <a class='notlink'>
    <i class="fas fa-edit fa-fw" aria-hidden="true"></i>
    <p>更新于：2020年4月26日</p>
  </a>
</div>

        
      
        
          
  
  <div class="new-meta-item meta-tags"><a class="tag" href="/tags/C%E8%AF%AD%E8%A8%80/" rel="nofollow"><i class="fas fa-hashtag fa-fw" aria-hidden="true"></i><p>C语言</p></a></div> <div class="new-meta-item meta-tags"><a class="tag" href="/tags/%E6%8C%87%E9%92%88/" rel="nofollow"><i class="fas fa-hashtag fa-fw" aria-hidden="true"></i><p>指针</p></a></div>


        
      
        
          

        
      
        
          
  <div class="new-meta-item share -mob-share-list">
  <div class="-mob-share-list share-body">
    
      
        <a class="-mob-share-qq" title="" rel="external nofollow noopener noreferrer"
          
          href="http://connect.qq.com/widget/shareqq/index.html?url=https://huqifa.com/posts/26235.html&title=为什么会有指针？[C语言] - 猿驿&summary=C语言指针学习"
          
          >
          
            <img src="/images/loading.gif" data-original="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/qq.png">
          
        </a>
      
    
      
        
        <div class='hoverbox'>
          <a><img src="/images/loading.gif" data-original="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/wechat.png"></a>
          <div class='target'>
            <img src="">
          </div>
        </div>
      
    
      
        <a class="-mob-share-qzone" title="" rel="external nofollow noopener noreferrer"
          
          href="https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://huqifa.com/posts/26235.html&title=为什么会有指针？[C语言] - 猿驿&summary=C语言指针学习"
          
          >
          
            <img src="/images/loading.gif" data-original="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/qzone.png">
          
        </a>
      
    
      
        <a class="-mob-share-weibo" title="" rel="external nofollow noopener noreferrer"
          
          href="http://service.weibo.com/share/share.php?url=https://huqifa.com/posts/26235.html&title=为什么会有指针？[C语言] - 猿驿&summary=C语言指针学习"
          
          >
          
            <img src="/images/loading.gif" data-original="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/weibo.png">
          
        </a>
      
    
      
        <a class="-mob-share-telegram" title="" rel="external nofollow noopener noreferrer"
          
            href="https://t.me/share/url?url=https://huqifa.com/posts/26235.html&text=为什么会有指针？[C语言] - 猿驿"
          
          >
          
            <img src="/images/loading.gif" data-original="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/telegram.png">
          
        </a>
      
    
  </div>
</div>



        
      
    </div>
  </section>


        
        
          <div class="prev-next">
            
              <a class='prev' href='/posts/36483.html'>
                <p class='title'><i class="fas fa-chevron-left" aria-hidden="true"></i>什么是链表？如何使用？[C语言]</p>
                <p class='content'>为什么会有链表？这门技术解决了什么问题？



或许这是让每一个IT初学者很头痛的问题，明明我们在之前已经接触了数组，感到数组已经是万能的数据存储位置了，但是，如果我们一直使用比较复杂的数据（也...</p>
              </a>
            
            
              <a class='next' href='/posts/50515.html'>
                <p class='title'>HashMap手动实现[Java]<i class="fas fa-chevron-right" aria-hidden="true"></i></p>
                <p class='content'>JavaHashMap底层原理_查找键值对过程





手动实现HashMappackage cn.Map;
/**
 * 自定义一个HashMap
 * 实现了put方法增加键值对，并解决了...</p>
              </a>
            
          </div>
        
      </section>
    </article>
  

  
    <!-- 显示推荐文章和评论 -->



  <article class="post white-box reveal comments shadow floatable">
    <section class="article typo">
      <p ct><i class='fas fa-comments'></i> 评论</p>
      
      
      
      
      
        <section id="comments">
          <div id="valine_container" class="valine_thread">
            <i class="fas fa-cog fa-spin fa-fw fa-2x"></i>
          </div>
        </section>
      
      
    </section>
  </article>


  




<!-- 根据页面mathjax变量决定是否加载MathJax数学公式js -->



  <script>
    window.subData = {
      title: '为什么会有指针？[C语言]',
      tools: true
    }
  </script>


</div>
<aside class='l_side'>
  
  

  
    
    

<section class="widget blogger shadow floatable desktop mobile">
  <div class='content'>
    
      
        <a class='avatar flat-box' href='/about/'>
          <img no-lazy src='https://gitee.com//BorenHU/BlogImage/raw/master/MarkDown/11.jpg'/>
        </a>
      
    
    
      <div class='text'>
        
          <h2>๑۩ﺴ HuqiA ﺴ۩๑</h2>
        
        
        
          <p><!--<span id="jinrishici-sentence">--><!--猿驿--><div id="binft"></div>
  <script>
    var binft = function (r) {
      function t() {
        return b[Math.floor(Math.random() * b.length)]
      }  
      function e() {
        return String.fromCharCode(94 * Math.random() + 33)
      }
      function n(r) {
        for (var n = document.createDocumentFragment(), i = 0; r > i; i++) {
          var l = document.createElement("span");
          l.textContent = e(), l.style.color = t(), n.appendChild(l)
        }
        return n
      }
      function i() {
        var t = o[c.skillI];
        c.step ? c.step-- : (c.step = g, c.prefixP < l.length ? (c.prefixP >= 0 && (c.text += l[c.prefixP]), c.prefixP++) : "forward" === c.direction ? c.skillP < t.length ? (c.text += t[c.skillP], c.skillP++) : c.delay ? c.delay-- : (c.direction = "backward", c.delay = a) : c.skillP > 0 ? (c.text = c.text.slice(0, -1), c.skillP--) : (c.skillI = (c.skillI + 1) % o.length, c.direction = "forward")), r.textContent = c.text, r.appendChild(n(c.prefixP < l.length ? Math.min(s, s + c.prefixP) : Math.min(s, t.length - c.skillP))), setTimeout(i, d)
      }
      var l = "",
      o = ["不怕念起,就怕觉迟", "理可顿悟,事须渐修"].map(function (r) {
      return r + ""
      }),
      a = 2,
      g = 1,
      s = 5,
      d = 75,
      b = ["rgb(110,64,170)", "rgb(150,61,179)", "rgb(191,60,175)", "rgb(228,65,157)", "rgb(254,75,131)", "rgb(255,94,99)", "rgb(255,120,71)", "rgb(251,150,51)", "rgb(226,183,47)", "rgb(198,214,60)", "rgb(175,240,91)", "rgb(127,246,88)", "rgb(82,246,103)", "rgb(48,239,130)", "rgb(29,223,163)", "rgb(26,199,194)", "rgb(35,171,216)", "rgb(54,140,225)", "rgb(76,110,219)", "rgb(96,84,200)"],
      c = {
        text: "",
        prefixP: -s,
        skillI: 0,
        skillP: 0,
        direction: "forward",
        delay: a,
        step: g
      };
      i()
      };
      binft(document.getElementById('binft'));
  </script></span></p>
          <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
        
      </div>
    
    
      <div class="social-wrapper">
        
          
            <a href="/atom.xml"
              class="social fas fa-rss flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
          
            <a href="https://gitee.com//BorenHU/BlogImage/raw/master/MarkDown/6.jpg"
              class="social fab fa-weixin flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
          
            <a href="https://www.zhihu.com/people/bo-v-60"
              class="social fab fa-zhihu flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
          
            <a href="https://github.com/BorenHU/BorenHU.github.io"
              class="social fab fa-github flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
          
            <a href="https://music.163.com/#/user/home?id=63035382"
              class="social fas fa-headphones-alt flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
      </div>
    
  </div>
</section>

  

  
    
    


  <section class="widget toc-wrapper shadow floatable desktop">
    
  <header>
    
      <i class="fas fa-list fa-fw" aria-hidden="true"></i><span class='name'>本文目录</span>
    
  </header>


    <div class='content'>
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#null"><span class="toc-number">1.</span> <span class="toc-text"></span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-为什么会有指针？这门技术解决了什么问题？"><span class="toc-number">1.1.</span> <span class="toc-text">1. 为什么会有指针？这门技术解决了什么问题？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-指针这项技术如何使用？"><span class="toc-number">1.2.</span> <span class="toc-text">2. 指针这项技术如何使用？</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#指针的算术运算"><span class="toc-number">1.2.1.</span> <span class="toc-text">指针的算术运算</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#运算符-amp-和"><span class="toc-number">1.2.2.</span> <span class="toc-text">*运算符&amp;和\ **</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#指针表达式"><span class="toc-number">1.2.3.</span> <span class="toc-text">指针表达式</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#数组和指针的关系"><span class="toc-number">1.2.4.</span> <span class="toc-text">数组和指针的关系</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#指针和结构类型的关系"><span class="toc-number">1.2.5.</span> <span class="toc-text">指针和结构类型的关系</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#指针和函数的关系"><span class="toc-number">1.2.6.</span> <span class="toc-text">指针和函数的关系</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#指针类型转换"><span class="toc-number">1.2.7.</span> <span class="toc-text">指针类型转换</span></a></li></ol></li></ol></li></ol>
    </div>
  </section>


  

  
    
    
  

  <section class="widget category shadow floatable desktop mobile">
    
  <header>
    
      <a href='/blog/categories/'><i class="fas fa-folder-open fa-fw" aria-hidden="true"></i><span class='name'>文章分类</span></a>
    
  </header>


    <div class='content'>
      <ul class="entry navigation">
        
          <li><a class="flat-box"
            title="/categories/C%E8%AF%AD%E8%A8%80/" href="/categories/C%E8%AF%AD%E8%A8%80/"
            id="categoriesCE8AFADE8A880"
            ><div class='name'>C语言</div><div class='badge'>(7)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/English/" href="/categories/English/"
            id="categoriesEnglish"
            ><div class='name'>English</div><div class='badge'>(1)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/Java/" href="/categories/Java/"
            id="categoriesJava"
            ><div class='name'>Java</div><div class='badge'>(32)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/LeetCode/" href="/categories/LeetCode/"
            id="categoriesLeetCode"
            ><div class='name'>LeetCode</div><div class='badge'>(2)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/Linux/" href="/categories/Linux/"
            id="categoriesLinux"
            ><div class='name'>Linux</div><div class='badge'>(1)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/MySql/" href="/categories/MySql/"
            id="categoriesMySql"
            ><div class='name'>MySql</div><div class='badge'>(1)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/Php/" href="/categories/Php/"
            id="categoriesPhp"
            ><div class='name'>Php</div><div class='badge'>(1)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/Self-investment/" href="/categories/Self-investment/"
            id="categoriesSelf-investment"
            ><div class='name'>Self-investment</div><div class='badge'>(2)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/Spring/" href="/categories/Spring/"
            id="categoriesSpring"
            ><div class='name'>Spring</div><div class='badge'>(1)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/Web%E5%89%8D%E7%AB%AF/" href="/categories/Web%E5%89%8D%E7%AB%AF/"
            id="categoriesWebE5898DE7ABAF"
            ><div class='name'>Web前端</div><div class='badge'>(3)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/WechatApplet/" href="/categories/WechatApplet/"
            id="categoriesWechatApplet"
            ><div class='name'>WechatApplet</div><div class='badge'>(1)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/"
            id="categoriesE695B0E68DAEE7BB93E69E84E4B88EE7AE97E6B395"
            ><div class='name'>数据结构与算法</div><div class='badge'>(6)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/" href="/categories/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/"
            id="categoriesE7BD91E7BB9CE7BC96E7A88B"
            ><div class='name'>网络编程</div><div class='badge'>(2)</div></a></li>
        
          <li><a class="flat-box"
            title="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%90%86%E8%AE%BA/" href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%90%86%E8%AE%BA/"
            id="categoriesE8AEA1E7AE97E69CBAE79086E8AEBA"
            ><div class='name'>计算机理论</div><div class='badge'>(3)</div></a></li>
        
      </ul>
    </div>
  </section>


  

  
    
    
  

  <section class="widget tagcloud shadow floatable desktop mobile">
    
  <header>
    
      <a href='/blog/tags/'><i class="fas fa-tags fa-fw" aria-hidden="true"></i><span class='name'>热门标签</span></a>
    
  </header>


    <div class='content'>
      <a href="/tags/C%E8%AF%AD%E8%A8%80/" style="font-size: 24px; color: #057777">C语言</a> <a href="/tags/English/" style="font-size: 14px; color: #018142">English</a> <a href="/tags/GUI/" style="font-size: 14px; color: #018142">GUI</a> <a href="/tags/Html/" style="font-size: 14px; color: #018142">Html</a> <a href="/tags/Java/" style="font-size: 34px; color: #086cac">Java</a> <a href="/tags/JavaEE/" style="font-size: 14px; color: #018142">JavaEE</a> <a href="/tags/JavaScript/" style="font-size: 14px; color: #018142">JavaScript</a> <a href="/tags/LeetCode/" style="font-size: 17.33px; color: #027e54">LeetCode</a> <a href="/tags/Linux/" style="font-size: 14px; color: #018142">Linux</a> <a href="/tags/MySql/" style="font-size: 14px; color: #018142">MySql</a> <a href="/tags/Php/" style="font-size: 14px; color: #018142">Php</a> <a href="/tags/Self-investment/" style="font-size: 17.33px; color: #027e54">Self-investment</a> <a href="/tags/Spring/" style="font-size: 14px; color: #018142">Spring</a> <a href="/tags/Web%E5%89%8D%E7%AB%AF/" style="font-size: 14px; color: #018142">Web前端</a> <a href="/tags/WechatApplet/" style="font-size: 14px; color: #018142">WechatApplet</a> <a href="/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" style="font-size: 30.67px; color: #07709a">多线程</a> <a href="/tags/%E6%8C%87%E9%92%88/" style="font-size: 17.33px; color: #027e54">指针</a> <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" style="font-size: 27.33px; color: #067389">数据结构与算法</a> <a href="/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/" style="font-size: 17.33px; color: #027e54">网络编程</a> <a href="/tags/%E8%99%9A%E6%8B%9F%E6%9C%BA/" style="font-size: 14px; color: #018142">虚拟机</a> <a href="/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%90%86%E8%AE%BA/" style="font-size: 20.67px; color: #037a65">计算机理论</a>
    </div>
  </section>


  

  
    
    
  

  <section class="widget related_posts shadow floatable desktop mobile">
    
  <header>
    
      <i class="fas fa-bookmark fa-fw" aria-hidden="true"></i><span class='name'>相关文章</span>
    
  </header>


    <div class="content">
      <ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="\posts\21343.html" title="如何使用指针？[C语言]" rel="bookmark">如何使用指针？[C语言]</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="\posts\23753.html" title="共用体类型[C语言]" rel="bookmark">共用体类型[C语言]</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="\posts\8314.html" title="结构体类型[C语言]" rel="bookmark">结构体类型[C语言]</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="\posts\53103.html" title="如何操作文件？[C语言]" rel="bookmark">如何操作文件？[C语言]</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a href="\posts\36483.html" title="什么是链表？如何使用？[C语言]" rel="bookmark">什么是链表？如何使用？[C语言]</a></h3></div></li></ul>
    </div>
  </section>


  


</aside>


  
  <footer class="clearfix">
    <br><br>
    
      
        <div class="aplayer-container">
          


        </div>
      
    
      
        <br>
        <div class="social-wrapper">
          
            
              <a href="/atom.xml"
                class="social fas fa-rss flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
            
              <a href="https://gitee.com//BorenHU/BlogImage/raw/master/MarkDown/6.jpg"
                class="social fab fa-weixin flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
            
              <a href="https://www.zhihu.com/people/bo-v-60"
                class="social fab fa-zhihu flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
            
              <a href="https://github.com/BorenHU/BorenHU.github.io"
                class="social fab fa-github flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
            
              <a href="https://music.163.com/#/user/home?id=63035382"
                class="social fas fa-headphones-alt flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
        </div>
      
    
      
        <div class='copyright'>
        <p><a href="https://huqifa.com">Copyright © 2020 HuqiA.blog</a> || <a href="http://www.beian.miit.gov.cn/" target="_blank">๑۩ﺴ 京ICP备19051116号-1 ﺴ۩๑</a> <br/> <span id="timeDate">载入天数…</span><span id="times">载入时分秒…</span> <br/>总访问量为<span id="busuanzi_value_site_pv">2462</span>次。 </p>

        </div>
      
    
  </footer>

<script>setLoadingBarProgress(80);</script>



<script>
    var now = new Date(); 
    function createtime() { 
        var grt= new Date("03/28/2020 00:00:00");//在此处修改你的建站时间，格式：月/日/年 时:分:秒
        now.setTime(now.getTime()+250); 
        days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days); 
        hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours); 
        if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum); 
        mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;} 
        seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum); 
        snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;} 
        document.getElementById("timeDate").innerHTML = "小站已安全运行 "+dnum+" 天 "; 
        document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒"; 
    } 
setInterval("createtime()",250);
</script>


      <script>setLoadingBarProgress(60);</script>
    </div>
    <a class="s-top fas fa-arrow-up fa-fw" href='javascript:void(0)'></a>
  </div>
  
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4/dist/jquery.min.js"></script>


  <script>
    
    var SEARCH_SERVICE = "hexo" || "hexo";
    var ROOT = "/" || "/";
    if (!ROOT.endsWith('/')) ROOT += '/';
  </script>


  <script async src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2/js/instant_page.js" type="module" defer integrity="sha384-OeDn4XE77tdHo8pGtE1apMPmAipjoxUQ++eeJa6EtJCfHlvijigWiJpD7VDPWXV1"></script>


  <script src="https://cdn.jsdelivr.net/npm/scrollreveal@4.0.6/dist/scrollreveal.min.js"></script>
  <script type="text/javascript">
    $(function() {
      ScrollReveal().reveal('.l_main .reveal', {
        distance: '8px',
        duration: '800',
        interval: '100',
        scale: '0.5'
      });
    });
  </script>


  
<script src="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.js"></script>

  <script type="text/javascript">
    $(function() {
      Waves.attach('.flat-btn', ['waves-button']);
      Waves.attach('.float-btn', ['waves-button', 'waves-float']);
      Waves.attach('.float-btn-light', ['waves-button', 'waves-float', 'waves-light']);
      Waves.attach('.flat-box', ['waves-block']);
      Waves.attach('.float-box', ['waves-block', 'waves-float']);
      Waves.attach('.waves-image');
      Waves.init();
    });
  </script>


  <script async src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-busuanzi@2.3/js/busuanzi.pure.mini.js"></script>



  
  
  
    
<script src="https://cdn.jsdelivr.net/npm/jquery-backstretch@2.1.18/jquery.backstretch.min.js"></script>

    <script type="text/javascript">
      $(function(){
        var imgs=["https://cdn.jsdelivr.net/gh/pic-cdn/cdn1@latest/2020/04/28/fb22c04ad5e72060ed615e9370b1aaf9.png"];
        if ('true' == 'true') {
          function shuffle(arr){
            /*From countercurrent-time*/
            var n = arr.length;
            while(n--) {
              var index = Math.floor(Math.random() * n);
              var temp = arr[index];
              arr[index] = arr[n];
              arr[n] = temp;
            }
          }
          shuffle(imgs);
        }
        if ('') {
          $('').backstretch(
            imgs,
          {
            duration: "20000",
            fade: "1500"
          });
        } else {
          $.backstretch(
            imgs,
          {
            duration: "20000",
            fade: "1500"
          });
        }
      });
    </script>
  










  
    
<script src="https://cdn.jsdelivr.net/npm/valine@1.4/dist/Valine.min.js"></script>

  
  <script>
  var GUEST_INFO = ['nick','mail','link'];
  var meta = 'nick,mail,link'.split(',').filter(function(item){
    return GUEST_INFO.indexOf(item) > -1
  });
  var REQUIRED_FIELDS = ['nick','mail','link'];
  var requiredFields = 'nick,mail'.split(',').filter(function(item){
    return REQUIRED_FIELDS.indexOf(item) > -1
  });
  var valine = new Valine();
  function emoji(path, idx, ext) {
      return path + "/" + path + "-" + idx + "." + ext;
  }
  var emojiMaps = {};
  for (var i = 1; i <= 54; i++) {
    emojiMaps['tieba-' + i] = emoji('tieba', i, 'png');
  }
  for (var i = 1; i <= 101; i++) {
    emojiMaps['qq-' + i] = emoji('qq', i, 'gif');
  }
  for (var i = 1; i <= 116; i++) {
    emojiMaps['aru-' + i] = emoji('aru', i, 'gif');
  }
  for (var i = 1; i <= 125; i++) {
    emojiMaps['twemoji-' + i] = emoji('twemoji', i, 'png');
  }
  for (var i = 1; i <= 4; i++) {
    emojiMaps['weibo-' + i] = emoji('weibo', i, 'png');
  }
  valine.init({
    el: '#valine_container',
    meta: meta,
    
    appId: "WVeTxOdAcxlExH2zkPceVLlW-gzGzoHsz",
    appKey: "k4cVjEnoyi9ULGaK4y7qz8PR",
    placeholder: "快来评论吧~",
    pageSize:'10',
    avatar:'robohash',
    lang:'zh-cn',
    visitor: 'true',
    highlight: 'true',
    mathJax: 'true',
    enableQQ: 'true',
    requiredFields: requiredFields,
    emojiCDN: 'https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/emoji/valine/',
    emojiMaps: emojiMaps
  })
  </script>





  
<script src="/js/app.js"></script>



  
<script src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2.4/js/search.js"></script>



  
<script src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2/js/comment_typing.js"></script>




  
    
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.18.1/build/highlight.min.js"></script>

    <script>hljs.initHighlightingOnLoad();</script>
  



<!-- 复制 -->

  <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="fas fa-copy"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('fa-copy');
        $icon.addClass('fa-check-circle');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('fa-check-circle');
          $icon.addClass('fa-copy');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('fa-copy');
        $icon.addClass('fa-times-circle');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('fa-times-circle');
          $icon.addClass('fa-copy');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>




<!-- fancybox -->

  <script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
<script>
  let LAZY_LOAD_IMAGE = "[object Object]";
  $(".article-entry").find("div.fancybox").find("img").each(function () {
      var element = document.createElement("a");
      $(element).attr("data-fancybox", "gallery");
      $(element).attr("href", $(this).attr("src"));
      /* 图片采用懒加载处理时,
       * 一般图片标签内会有个属性名来存放图片的真实地址，比如 data-original,
       * 那么此处将原本的属性名src替换为对应属性名data-original,
       * 修改如下
       */
       if (LAZY_LOAD_IMAGE) {
         $(element).attr("href", $(this).attr("data-original"));
       }
      $(this).wrap(element);
  });
</script>






  <script>setLoadingBarProgress(100);</script>

        <script src="//cdn.jsdelivr.net/npm/js-base64/base64.min.js"></script>
        <script>
        $('a').each(function() {
          const $this = $(this);
          const href = $this.attr('href');
          if (href && href.match('^((http|https|thunder|qqdl|ed2k|Flashget|qbrowser|ftp|rtsp|mms)://)')) {
            const strs = href.split('/');
            if (strs.length >= 3) {
                const host = strs[2];
                if (host !== 'your_domain' || window.location.host) {
                    $this.attr('href', '/go.html?u='+href+'').attr('rel', 'external nofollow noopener noreferrer');
                    if (true) {
                        $this.attr('target', '_blank');
                    }
                }
            }
          }
        });
        </script><script>!function(e){var c=Array.prototype.slice.call(document.querySelectorAll("img[data-original]"));function i(){for(var r=0;r<c.length;r++)t=c[r],0<=(n=t.getBoundingClientRect()).bottom&&0<=n.left&&n.top<=(e.innerHeight||document.documentElement.clientHeight)&&function(){var t,n,e,i,o=c[r];t=o,n=function(){c=c.filter(function(t){return o!==t})},e=new Image,i=t.getAttribute("data-original"),e.onload=function(){t.src=i,n&&n()},e.src=i}();var t,n}i(),e.addEventListener("scroll",function(){var t,n;t=i,n=e,clearTimeout(t.tId),t.tId=setTimeout(function(){t.call(n)},500)})}(this);</script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script></body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/love.js"></script>